// 分页/回到顶部 功能
(function ($) {
    var opt = {
        startPos: 0,    //起点位置.计算中被减去的部分
        stepHeight: 100,
        always: false,
        time: 1100,
        total: 10
    };

    var setCur = function (num) {

        if (num < 1) {
            num = 1;
        }

        $(opt.element).find('.ac-page-cur').text(num);
    };

    var init = function (element, option) {
        opt = $.extend({}, opt, option);
        opt.element = element;
        //初始化到第一页
        setCur(1);
        //初始化总页数
        $(element).find('.ac-page-total').text(opt.total);
    };


    var delayFuc = function (callback) {
        return setTimeout(function () {
            callback && callback()
        }, opt.time);
    };

    var index;

    var isShow = true;

    var isNav = true;

    var cutNavTask;

    $.fn.onScroll = function (posValue) {


        //设置当前页数
        index = Math.floor((posValue-opt.startPos) / opt.stepHeight) + 1;
        setCur(index);


        clearTimeout(hideTask);
        if (!isShow) {
            $(opt.element).show();
            isShow = true;
        }

        clearTimeout(cutNavTask);
        if (!isNav) {
            $(opt.element).find('.ac-page-nav').show();
            $(opt.element).find('.ac-page-top').hide();
            isNav = true;
        }


    };


    var hideTask;


    $.fn.scrollEnd = function (posValue) {

        index = Math.floor((posValue - opt.startPos) / opt.stepHeight) + 1;

        setCur(index);

        if (!isShow) {
            clearTimeout(cutNavTask);
            $(opt.element).show();
            isShow = true;
        }

        if (!isNav) {
            //显示页码图标
            $(opt.element).find('.ac-page-nav').show();
            $(opt.element).find('.ac-page-top').hide();
            isNav = true;
        }


        if (index === 1) {//回到顶端
            if (!opt.always) {

                clearTimeout(hideTask);
                hideTask=delayFuc(function () {//在这里延时执行隐藏
                    //再进行一次判断
                    if (index === 1) {
                        if (isShow) {
                            $(opt.element).hide();
                            isShow = false;
                        }
                    }
                })
            }
        }
        else {

            clearTimeout(hideTask);
            cutNavTask=delayFuc(function () {
                if (index != 1) {
                    //隐藏导航栏.显示回到顶部
                    if (isNav) {

                        $(opt.element).find('.ac-page-nav').hide();
                        $(opt.element).find('.ac-page-top').show().css('opacity', '1');

                        isNav = false;

                        clearTimeout(hideTask);
                        hideTask=delayFuc(function () {
                            if (isShow) {
                                $(opt.element).hide();
                                isShow = false;
                            }
                        });


                    }
                }
            })
        }

    };

    $.fn.totalAdd = function (num) {
        var c=$(opt.element).find('.ac-page-total').text();

        $(opt.element).find('.ac-page-total').text(parseInt(c)+num);
    };
    $.fn.totalSub = function (num) {
        var c=$(opt.element).find('.ac-page-total').text();
        $(opt.element).find('.ac-page-total').text(parseInt(c)-num);
    };

    $.fn.updateTotal = function (t) {
        $(opt.element).find('.ac-page-total').text(t);
    };

    opt.updateTotal = function (total) {
        opt.total = total;
        $(opt.element).find('.ac-page-total').text(total);
    };
    opt.updateCul = function (cur) {
        $(opt.element).find('.ac-page-cur').text(cur);
    };

    $.fn.pageination = function (option) {
        var that = this;
        init(that, option);
        return opt;
    };


}($));

var refresher = {
    info: {
        "pullDownLable": "下拉刷新",
        "pullingDownLable": "松开刷新",
        "pullUpLable": "上拉加载更多",
        "pullingUpLable": "松开加载",
        "loadingLable": "加载中..."
    },
    init: function (parameter) {
        var wrapper = document.getElementById(parameter.id);

        var div = document.createElement("div");
        div.id = "scroller";
        wrapper.appendChild(div);

        var scroller = document.getElementById("scroller");
        var list = document.querySelector("#" + parameter.id + " ul")
        scroller.insertBefore(list, scroller.childNodes[0]);

        var pullDown = document.createElement("div");
        pullDown.id = "pullDown";
        var loader = document.createElement("div");
        loader.className = "loader";
        for (var i = 0; i < 4; i++) {
            var span = document.createElement("span");
            loader.appendChild(span);
        }
        pullDown.appendChild(loader);

        var pullDownLabel = document.createElement("div");
        pullDownLabel.className = "pullDownLabel";
        pullDown.appendChild(pullDownLabel);

        scroller.insertBefore(pullDown, scroller.childNodes[0]);

        var pullUp = document.createElement("div");
        pullUp.id = "pullUp";
        var loader = document.createElement("div");
        loader.className = "loader";
        for (var i = 0; i < 4; i++) {
            var span = document.createElement("span");
            loader.appendChild(span);
        }

        pullUp.appendChild(loader);

        var pullUpLabel = document.createElement("div");
        pullUpLabel.className = "pullUpLabel";
        pullUpLabel.style = "height: 60px";
        var content = document.createTextNode(refresher.info.pullUpLable);
        pullUpLabel.appendChild(content);
        pullUp.appendChild(pullUpLabel);

        scroller.appendChild(pullUp);
        //create dom above
        //create dom ,you can wirte it yourself
        var pullDownEl = document.getElementById('pullDown');
        var pullDownOffset = pullDownEl.offsetHeight;
        var pullUpEl = document.getElementById('pullUp');
        var pullUpOffset = pullUpEl.offsetHeight;
        //parameter
        this.scrollIt(parameter, pullDownEl, pullDownOffset, pullUpEl, pullUpOffset);
    },

    scrollIt: function (parameter, pullDownEl, pullDownOffset, pullUpEl, pullUpOffset) {
        myScroll = new iScroll(parameter.id, {
            useTransition: true,
            vScrollbar: false, //hide the iscroll v bar
            topOffset: pullDownOffset,
            onRefresh: function () {
                refresher.onRelease(pullDownEl, pullUpEl);
            },
            onScrollMove: function () {

                //parameter.pagger.onScroll(-this.y);

                parameter.scroll(-this.y);

                refresher.onScrolling(this, pullDownEl, pullUpEl, pullUpOffset);//element
            },
            onScrollEnd: function () {

                //parameter.pagger.scrollEnd(-this.y);

                parameter.scrollEnd(-this.y);

                refresher.onPulling(pullDownEl, parameter.pullDownAction, pullUpEl, parameter.pullUpAction);
            }
        });
/*
        parameter.pagger.on('click.top', function (event) {
            parameter.pagger.onScroll(myScroll.startY);
            myScroll.scrollTo(0, myScroll.minScrollY, 1000, false);
        });*/

        setTimeout(function () {
            pullDownEl.querySelector('.pullDownLabel').innerHTML = refresher.info.pullDownLable
        }, 300);
        document.addEventListener('touchmove', function (e) {
        }, false);


        this.myScroll = myScroll;

    },

    //滚动时的回调
    onScrolling: function (e, pullDownEl, pullUpEl, pullUpOffset) {

        if (e.y > -(pullUpOffset)) {
            pullDownEl.className = '';
            pullDownEl.querySelector('.pullDownLabel').innerHTML = refresher.info.pullDownLable;
            e.minScrollY = -pullUpOffset;
        }
        if (e.y > 0) {
            pullDownEl.className = 'flip';
            pullDownEl.querySelector('.pullDownLabel').innerHTML = refresher.info.pullingDownLable;
            e.minScrollY = 0;
        }
        if (e.scrollerH < e.wrapperH && e.y < (e.minScrollY - pullUpOffset) || e.scrollerH > e.wrapperH && e.y < (e.maxScrollY - pullUpOffset)) {
            document.getElementById("pullUp").style.display = "block";
            pullUpEl.className = 'flip';
            pullUpEl.querySelector('.pullUpLabel').innerHTML = refresher.info.pullingUpLable;
        }
        if (e.scrollerH < e.wrapperH && e.y > (e.minScrollY - pullUpOffset) && pullUpEl.className.match('flip') || e.scrollerH > e.wrapperH && e.y > (e.maxScrollY - pullUpOffset) && pullUpEl.className.match('flip')) {
            document.getElementById("pullUp").style.display = "none";
            pullUpEl.className = '';
            pullUpEl.querySelector('.pullUpLabel').innerHTML = refresher.info.pullUpLable;
        }


    },

    //release方法的回调
    onRelease: function (pullDownEl, pullUpEl) {
        if (pullDownEl.className.match('loading')) {
            pullDownEl.className = '';
            pullDownEl.querySelector('.pullDownLabel').innerHTML = refresher.info.pullDownLable;
            pullDownEl.querySelector('.loader').style.display = "none";
            pullDownEl.style.lineHeight = pullDownEl.offsetHeight + "px";
        }
        if (pullUpEl.className.match('loading')) {
            pullUpEl.className = '';
            pullUpEl.querySelector('.pullUpLabel').innerHTML = refresher.info.pullUpLable;
            pullUpEl.querySelector('.loader').style.display = "none";
            pullUpEl.style.lineHeight = pullDownEl.offsetHeight + "px";
        }
    },
    //loader方法的回调
    onPulling: function (pullDownEl, pullDownAction, pullUpEl, pullUpAction) {
        if (pullDownEl.className.match('flip')) {
            pullDownEl.className = 'loading';
            pullDownEl.querySelector('.pullDownLabel').innerHTML = refresher.info.loadingLable;
            pullDownEl.querySelector('.loader').style.display = "block";
            pullDownEl.style.lineHeight = "20px";
            if (pullDownAction)
                pullDownAction();	// Execute custom function (ajax call?)
        }
        if (pullUpEl.className.match('flip')) {
            pullUpEl.className = 'loading';
            pullUpEl.querySelector('.pullUpLabel').innerHTML = refresher.info.loadingLable;
            pullUpEl.querySelector('.loader').style.display = "block";
            pullUpEl.style.lineHeight = "20px";
            if (pullDownAction)
                pullUpAction();	// Execute custom function (ajax call?)
        }
    }

};
